
#include <boost/test/test_tools.hpp>
#include <boost/exception.hpp>
#include <iostream>
#include <vector>
#include <stdexcept>

using std::vector;
using std::invalid_argument;

#include "../src/Plan.hpp"
using fixstoker::Plan;
using fixstoker::PlanEntry;
using fixstoker::Substitution;
using namespace boost;

void
testCreatePerfRunPlan() {

	Plan plan;

	plan.addMessageTemplate("8=123|10=2222|333=333|10=333|");
	plan.addMessageTemplate("8=123|10=2222|333=333|10=333|");

	plan.addEntry(new PlanEntry(0,
			new Substitution(10, 20, "oops0")));

	plan.addEntry(new PlanEntry(1,
			new Substitution(10, 20, "oops1")));

	plan.addEntry(new PlanEntry(1,
				new Substitution(10, 20, "oops2")));

	BOOST_CHECK_EQUAL(2, plan.numberOfMessageTemplates());
	BOOST_CHECK_EQUAL(3, plan.entries().size());
}

void
testCreatePerfRunPlanWith2Substitutions() {
	Plan plan;

	plan.addMessageTemplate("8=123|10=2222|333=333|10=333|");

	vector<const Substitution*> substitutions;
	substitutions.push_back(new Substitution(10, 20, "oops"));
	substitutions.push_back(new Substitution(11, 20, "oops1"));

	plan.addEntry(new PlanEntry(0, substitutions));

	BOOST_CHECK_EQUAL(1, plan.numberOfMessageTemplates());
	BOOST_CHECK_EQUAL(1, plan.entries().size());
	const PlanEntry* entry = plan.entries()[0];
	BOOST_CHECK_EQUAL(2, entry->substitutions().size());
}

void
testShouldThrowIfReferenceToUnExistingMessageTemplate() {
	// GIVEN
	Plan plan;

	plan.addMessageTemplate("8=123|10=2222|333=333|10=333|");

	plan.addEntry(new PlanEntry(0,
				new Substitution(10, 20, "oops0")));

	// WHEN
	bool gotException = false;
	std::string errMsg;

	try {
		plan.addEntry(new PlanEntry(1,
					new Substitution(10, 20, "oops1")));
		BOOST_FAIL("std::invalid_argument exception expected");
	} catch (const boost::exception& e) {
		//std::cerr << diagnostic_information(e);
		errMsg = boost::diagnostic_information(e);
		gotException = true;
	}

	// THEN
	BOOST_CHECK(true == gotException);
	BOOST_CHECK(errMsg.find("Invalid argument:") > 0);
}

void
testCreateEmptyPerfRunPlan() {
	Plan plan;
	BOOST_CHECK_EQUAL(0, plan.entries().size());
}




